package customs

import (
	"wxsdk/wxpay/utils/mapstruct"
	"github.com/astaxie/beego"
)

type MircopayRequest struct {
	AppId         string   `xml:"appid,omitempty"`          //公众账号ID
	MchId         string   `xml:"mch_id,omitempty"`         //商户号
	Customs       string   `xml:"customs,omitempty"`        //海关
	MchCustomsNo  string   `xml:"mch_customs_no,omitempty"` //商户海关备案号
	OrderFee      string   `xml:"order_fee,omitempty"`      //应付金额
	OutTradeNo    string   `xml:"out_trade_no,omitempty"`   //商户订单号
	ProductFee    string   `xml:"product_fee,omitempty"`    //商品价格
	TransactionId string   `xml:"transaction_id,omitempty"` //微信支付订单号
	TransportFee  string   `xml:"transport_fee,omitempty"`  //物流费
	FeeType       string   `xml:"fee_type,omitempty"`       //币种
	SubOrderNo    string   `xml:"sub_order_no,omitempty"`   //商户子订单号
	CertType      string   `xml:"cert_type,omitempty"`      //证件类型
	CertId        string   `xml:"cert_id,omitempty"`        //身份证号
	Name          string   `xml:"name,omitempty"`           //用户姓名
	Sign          string   `xml:"sign,omitempty"`           //签名
}

type MircopayResponse struct {
    CustomsBaseResponse `xml:",innerXml,omitempty"`
	State           string `xml:"state,omitempty"`             //状态码
	TransactionId   string `xml:"transaction_id,omitempty"`    //微信支付订单号
	OutTradeNo      string `xml:"out_trade_no,omitempty"`      //商户订单号
	SubOrderNo      string `xml:"sub_order_no,omitempty"`      //商户子订单号
	SubOrderId      string `xml:"sub_order_id,omitempty"`      //微信子订单号
	ModifyTime      string `xml:"modify_time,omitempty"`       //最后更新时间
	CertCheckResult string `xml:"cert_check_result,omitempty"` //订购人和支付人身份信息校验结果
}

func MicroAppPay(appSecret string, request *MircopayRequest)(rsp *MircopayResponse, err error){
	reqURL := "https://api.mch.weixin.qq.com/cgi-bin/mch/customs/customdeclareorder"
	sdk := NewSDK(reqURL, request.AppId, request.MchId, appSecret, nil)
	params := mapstruct.Struct2Map(request, "xml")
	rspMap, e := sdk.Execute(params)
	if e != nil {
	   beego.Debug("MicroAppPay sdk.Execute:", e.Error())
	   err = e
	   return
	}
	microRsp := new(MircopayResponse)
	err = mapstruct.Map2Struct(rspMap, microRsp, "xml")
	if err == nil {
	   rsp = microRsp
	}
	return
}

